package utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Validador {

	public static boolean esVacio(String s) {
		return s.length()==0;
	}
	//Puede ser una cadena o un array --> de ahi que sea un object como parametro
	public static boolean esNulo(Object o) {
		return o==null;
	}
	public static boolean esCorreoElectronico(String s){
		boolean exito = false;
		//No empezar ni por @ ni por pto.
		Pattern p1 = Pattern.compile("^\\.|^\\@");
	    Matcher m1 = p1.matcher(s);
	   
	    //que no contenga "www." ni "http:\\"
	    Pattern p2 = Pattern.compile("^www\\.|^http\\:\\\\");
	    Matcher m2 = p2.matcher(s);
	    
	    Pattern p3 = Pattern.compile(".+@.+\\.[a-z]+");
	    Matcher m3 = p3.matcher(s);
	    
	    if(!m1.find() && !m2.find() && m3.find()){
	    	exito=true;
	    }
	    
		return exito;
	}
	
	public static boolean esURLValido(String s){
		Pattern p = Pattern.compile("^http(s?)://((www.)?)[A-Za-z]+(\\.[A-Za-z0-9])?");
		Matcher m = p.matcher(s);
		return m.find();
	}
	/**
	 * DEPURAR PARA LOS DOS PRIMEROS CASOS
	 * @param s
	 * @return
	 */
	public static boolean esNumeroTelefono(String s){
		
		String patronTfno1 = "(\\+\\?\\s*\\([0-9][0-9]\\)\\s*[^0-9]+)";
		String patronTfno2 = "(\\+\\?\\s*[0-9][0-9]\\s*[^0-9]+)";
		String patronTfno3 = "[^0-9\\s]+";
		
		Pattern p1 = Pattern.compile(patronTfno1 + "|" + patronTfno2);
		Pattern p2 = Pattern.compile(patronTfno3);
		Matcher m1 = p1.matcher(s);
		Matcher m2 = p2.matcher(s);
		System.out.println("Cadena s:" + s + ";m1->" + m1.find() + ";m2->" + m2.find());
		return m1.find() || !m2.find();
	}
	public static boolean hayInyeccionSQL(String s){
		s = s.toUpperCase();
		Pattern p = Pattern.compile(";(\\s)*((INSERT)|(DROP)|(CREATE)|(TRUNCATE)|(ALTER))");
		Matcher m = p.matcher(s);
		return m.find();
	}
	
	public static void main(String[] args) {

	}
	
}
